package com.example.kuaizu.config;

import lombok.extern.log4j.Log4j2;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.HttpURLConnection;


/**
 * @author: wangmx15
 * @date: 2025/8/30
 **/
@Configuration
@Log4j2
public class RestTemplateConfig {

    @Bean // 单例Bean，确保所有线程（包括MQ线程）复用同一配置
    public RestTemplate restTemplate() {
        try {
            //// 1. 初始化SSL上下文（使用标准TLSv1.2协议，兼容性更好）
            //SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            //// 2. 初始化SSL上下文（无自定义证书时，使用JVM默认的信任管理器和密钥管理器）
            //sslContext.init(null, null, null);
            //// 3. 获取SSLSocketFactory（此时一定不为null，因为初始化已捕获异常）
            //SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            //
            //// 4. 配置请求工厂，显式设置SSLSocketFactory
            //SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            //// 关键：强制绑定SSL工厂
            //requestFactory.setSslSocketFactory(sslSocketFactory);
            //// 可选：设置连接超时
            //requestFactory.setConnectTimeout(5000);
            //// 可选：设置读取超时
            //requestFactory.setReadTimeout(5000);
            //
            //// 5. 创建RestTemplate并返回
            //return new RestTemplate(requestFactory);
            // 1. 初始化SSL上下文（与之前相同）
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, null, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            // 2. 创建请求工厂，并通过 customizer 配置SSL
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() {
                @Override
                protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
                    // 若为HTTPS连接，强制设置SSLSocketFactory
                    if (connection instanceof HttpsURLConnection) {
                        ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
                    }
                    super.prepareConnection(connection, httpMethod);
                }
            };

            // 3. 设置超时时间（可选）
            requestFactory.setConnectTimeout(5000);
            requestFactory.setReadTimeout(5000);

            // 4. 创建RestTemplate
            return new RestTemplate(requestFactory);
        } catch (Exception e) {
            // 初始化失败直接抛异常，避免后续使用时出现NullPointerException
            throw new RuntimeException("RestTemplate SSL配置初始化失败", e);
        }
    }

    //@Bean
    //public static SSLSocketFactory getCustomSSLSocketFactory() {
    //    try {
    //        // 显式初始化 SSL 上下文（使用 TLSv1.2 协议，兼容性更稳定）
    //        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    //        sslContext.init(null, null, null); // 使用默认的信任管理器和密钥管理器
    //        return sslContext.getSocketFactory();
    //    } catch (Exception e) {
    //        throw new RuntimeException("SSL 上下文初始化失败", e);
    //    }
    //}
}
